function Private_question() {
    var _this = this;
    var page;
    var list_url = appPath + "/popup/course_question_list";
    _this.obj = null;
    _this.templateArr = [];
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTable();
        _this.initEvent(Object);
        _this.tableInit();
        _this.selectedQuestion();
    }

    _this.initTable = function() {
        _this.obj = null;
        page.init("question_required_form", list_url, "question_required_list", "question_required_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            //$("#checkAll").prop("checked",false);
            _this.obj = dataList;
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {

                item = dataList[i];
                inner += '<tr >';
              if(_this.selected(item.intId)){
                inner += '<td width="50" class="checked"><input type=\"checkbox\" name="courseBox" value="' + item['intId'] + '" data-id="' + item['intId'] + '"></td>';
              }else{
                inner += '<td width="50"><input type=\"checkbox\" name="courseBox" value="' + item['intId'] + '"  data-id="' + item['intId'] + '"></td>';
              }
                inner += '<td width="350">'+ item['varContent'] + '</td>';
                inner += '<td width="80">'+ item['chrType'] + '</td>';
                inner += '</tr>';
            }

            return inner;
        }
        _this.tableInit();
    };

    //初始化选择后的表格
    _this.tableInit = function(){
        $('#question_required_list_copy').html('<tr><td colspan="3" class="empty">暂无数据</td></tr>')
    }


    _this.initEvent = function () {
        // 确定
        $("#btn_sure").click(function () {
            // if(_this.templateArr.length>0){
                var isCenterTrain = $("#isCenterTrain", window.parent.document).val();
                var value=$("#parentSource",window.parent.document).val();
                submitQusetion(_this.templateArr, isCenterTrain);
            // }
            _this.closeDialog();
        });

        //取消ss
        $("#btn_cancel").click(function () {
            _this.closeDialog();
        });
        $('#question_required_list').on('change','input',function(){
            var $this = $(this);
            var id = $this.attr('data-id');
          // alert( _this.getIndex(_this.obj, 'intId', id) );
        })

        //选择课程
        $('#question_required_list').on('change','input',function(){
            var $this = $(this);
            var flag = $this.prop('checked');
            var id = $this.attr('data-id');
            var index = _this.getIndex(_this.obj, id);
            var template_index = _this.getIndex(_this.templateArr, id);
            if( flag && index != -1  ){

               // _this.templateArr.push(_this.obj[index]);

                _this.removeRepart(_this.obj[index])

            }else if( !flag && template_index != -1 ){

                _this.templateArr.splice( template_index,1 );

            }
            _this.select_after();
        });

    }

    //关闭对话框
    _this.closeDialog =function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }
    //获取id索引
    _this.getIndex = function(obj, attr){
        var index = -1;
        $.each(obj,function(i, n){
            if( this.intId == attr ){
                index = i;
                return false;
            }
        })
        return index;
    }
    //选择课程后执行
    _this.select_after = function(){
        var tbody = '';
        $.each(_this.templateArr,function(i, n){
            tbody += '<tr >';
            tbody += '<td width="50"  >' +
                '<input type=\"checkbox\" checked="checked"  onclick="private_question.changeQuestion(\''+this.intId+'\')" ' +
                'name="questionBox" value="' + this.intId + '" data-id="' +this.intId + '"></td>';
            /*
            tbody += '<td><input type="hidden" data-id="' + this.intId + '"></td>';*/
            tbody += '<td width="350">'+ this.varContent + '</td>';
            tbody += '<td  width="80">'+ this.chrType + '</td>';
            tbody += '</tr>';
        })
        $('#question_required_list_copy').html( tbody );
    }

    //搜索课程
    _this.search = function() {
        //$("#checkAll").prop("checked",false);
        var _this = private_question;
      $("#name_search").val($.trim($("#name_search").val()));
        _this.initTable();
        //展示选中课程
        _this.select_after();
    };

    //全部
    _this.all = function() {
        $("#chrType").val("");
        $("#name_search").val("");
        _this.initTable();

        //展示选中课程
        _this.select_after();
    };
    //全选
    _this.checkAll=function () {
        var flag=$("#all").prop("checked");
        $('tbody input[name="courseBox"]').prop("checked",flag);
        if(flag && _this.obj.length>0){
            $.each(_this.obj,function(i, n){
                _this.removeRepart(this);
            })
        }else if( !flag ){

            $.each(_this.obj,function(i, n){
                var id = this.intId;
                var template_index = _this.getIndex(_this.templateArr, id);
                if(-1!=template_index){
                _this.templateArr.splice( template_index,1 );
                }
            })

        }
        _this.select_after();
    };



      //是否选中
      _this.selected = function(courseId){
        for(var m=0; m<_this.templateArr.length; m++){
          if(_this.templateArr[m].intId == courseId){
            return true;
          }
        }
        return false;
      };

    //去重
    _this.removeRepart=function (courseData) {
        var flag=true;
        for (var i=0;i<_this.templateArr.length;i++){
            if(_this.templateArr[i].intId==courseData.intId){
                flag=false;
            }
        }
        if(flag){
            _this.templateArr.push(courseData) ;
        }
    }

    // 选择培训课程的回调
    submitQusetion=function(rows, isCenterTrain){
        var questionIds = [];
        for(var i=0; i<rows.length; i++){
            questionIds.push(rows[i].intId);
        }
        var roles = [];
        var obj=new Object();
        obj.roleId="-1";
        obj.roleName="默认角色";
        roles.push(obj);
        var projectId =  $("#projectId").val();
        var courseId = $("#courseId").val();

        roles =JSON.stringify(roles);

        // 选择培训课程
        $.ajax({
            url: appPath + '/admin/project_create/private/saveProjectQuestion',
            async: false,
            type: 'post',
            data: {
                'projectId':projectId,                                  // 项目id                 // 试题ids
                'questionIds': questionIds.join(","),
                'courseId': courseId,                                  // 培训课程id
            },
            success: function(data){
                var code = data['code'];
                if(code==10000){
                    /*layer.alert('操作成功', {icon: 1,  skin: 'layer-ext-moon'}, function(index){
                        layer.close(index);*/
                    //加载课程列表信息
                    parent.step2.project_role_tree_node_course();

                    // });
                }else{
                    layer.alert('操作失败', {icon: 2,  skin: 'layer-ext-moon'});
                }
            }
        });
    }


// 查询选中的课程
    _this.selectedQuestion=function(){
        var projectId =  $("#projectId").val();
        var courseId = $("#courseId").val();

        // 选择培训课程
        $.ajax({
            url: appPath + '/popup/required_question_list',
            async: false,
            type: 'post',
            data: {
                'projectId':projectId,                                  // 项目id
                'courseId': courseId,                                    // 培训课程id
                'pageNum': 1,                                   // 当前页
                'pageSize': 10000,                                 // 总量
            },
            success: function(data){
                var code = data['code'];
                if(code==10000){
                    _this.templateArr = data['result'];
                    if(null == _this.templateArr){
                        _this.templateArr = [];
                    }
                    _this.select_after();
                }
            }
        });
    }

    //是否选中
    _this.changeQuestion = function(questionId){
        for(var m=0; m<_this.templateArr.length; m++){
            if(_this.templateArr[m].intId == questionId){
                _this.templateArr.splice(m,1);
            }
        }
        $('tbody input[data-id = '+questionId+']').prop("checked",false);
        _this.select_after();
    };

    //全选
    _this.checkQuestionAll=function () {
        var flag=$("#questionAll").prop("checked");
        $('tbody input[name="questionBox"]').prop("checked",flag);
        if( !flag ){

            $.each(_this.obj,function(i, n){
                var id = this.intId;
                var template_index = _this.getIndex(_this.templateArr, id);
                if(-1!=template_index){
                    _this.templateArr.splice( template_index,1 );
                }
            })

            $('tbody input[name= courseBox]').prop("checked",false);
        }
        _this.select_after();
    };

}

var private_question = new Private_question();
